/* 
 * File:   View.cpp
 * Author: chb
 * 
 * Created on 4. September 2009, 20:59
 */

#include "View.h"
#include <iostream>

using namespace std;

View* View::_instance = 0;

View::View() {
	init();

}
View* View::instance(){
	if(_instance == 0)
		_instance = new View;
	return _instance;
}

void View::drawGraphic(const Position* p,  string graphic) throw (exception) {
	SDL_Surface *g;
        
	g = SDL_LoadBMP(string("src/" + graphic + ".bmp").c_str());

	if(g == 0) {
		throw exception();
	}

	SDL_Rect dst;

	dst.x = p->getPixelX();
	dst.y = p->getPixelY();
	dst.w = g->w;
	dst.h = g->h;

        SDL_SetColorKey(g, SDL_SRCCOLORKEY, SDL_MapRGB(g->format, 0, 0, 0) );

	SDL_BlitSurface(g, NULL, _screen, &dst);

	SDL_Flip(_screen);
}

// private:

void View::init(){
	SDL_Init(SDL_INIT_VIDEO);
	_screen = SDL_SetVideoMode(640,480,32,SDL_HWSURFACE|SDL_DOUBLEBUF); //SDL_FULLSCREEN|
	SDL_FillRect(_screen, NULL, SDL_MapRGB(_screen->format,0,0,0));
}

void View::uninit(){
	SDL_Quit();
}

View::~View() {
	uninit();
	cout << "uninit complete..." << endl;
}
